package com.j3l11234.myy_sign.model;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.j3l11234.myy_sign.entity.SettingEntity;
import com.j3l11234.myy_sign.util.MyException;

@Service
@Scope("singleton")
public class SettingModel {
	@Autowired
	private SessionFactory sessionFactory;
	
	private static final String BOARD_TEXT = "board_text";
	private static final String SIGNIN_TIME = "signin_time";
	private static final String SIGNOUT_TIME = "signout_time";
	
	private DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss"); 

	
	public String getBoardText() throws MyException{
		Session session = null;
		String boardText = "";
		try {
			session = sessionFactory.openSession();	
			Criteria criteria = session.createCriteria(SettingEntity.class); 
			criteria.add(Restrictions.eq("key", BOARD_TEXT)); 
			criteria.uniqueResult();
			
			SettingEntity setting = (SettingEntity) criteria.uniqueResult();
			if(setting != null){
				boardText = setting.getValue();
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new MyException("数据库访问错误");
		}finally{
			if(session != null){
				session.close();
			}	
		}
		
		return boardText;
	}
	
	public void setBoardText(String boardText) throws MyException{
		Session session = null;
		SettingEntity setting = new SettingEntity();
		try {
			session = sessionFactory.openSession();	
			Transaction tran = session.beginTransaction();  
			setting.setKey(BOARD_TEXT);
			setting.setValue(boardText);
			session.update(setting);
			tran.commit();
		} catch (Exception e) {
			e.printStackTrace();
			throw new MyException("数据库访问错误");
		}finally{
			if(session != null){
				session.close();
			}	
		}
	}
	
	
	public Date getSigninTime() throws MyException {
		Session session = null;
		Date signinTime = null;
		try {
			signinTime = timeFormat.parse("08:00:00");
			session = sessionFactory.openSession();	
			Criteria criteria = session.createCriteria(SettingEntity.class); 
			criteria.add(Restrictions.eq("key", SIGNIN_TIME)); 
			criteria.uniqueResult();
			
			SettingEntity setting = (SettingEntity) criteria.uniqueResult();
			if(setting != null){
				signinTime = timeFormat.parse(setting.getValue());
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new MyException("数据库访问错误");
		}finally{
			if(session != null){
				session.close();
			}	
		}
		return signinTime;
	}
	
	public void setSigninTime(Date signinTime) throws MyException {
		Session session = null;
		SettingEntity setting = new SettingEntity();
		try {
			session = sessionFactory.openSession();	
			Transaction tran = session.beginTransaction();  
			setting.setKey(SIGNIN_TIME);
			setting.setValue(timeFormat.format(signinTime));
			session.update(setting);
			tran.commit();
		} catch (Exception e) {
			e.printStackTrace();
			throw new MyException("数据库访问错误");
		}finally{
			if(session != null){
				session.close();
			}	
		}
	}
	
	public Date getSignoutTime() throws MyException {	
		Session session = null;
		Date signoutTime = null;
		try {
			signoutTime = timeFormat.parse("17:00:00");
			session = sessionFactory.openSession();	
			Criteria criteria = session.createCriteria(SettingEntity.class); 
			criteria.add(Restrictions.eq("key", SIGNOUT_TIME)); 
			criteria.uniqueResult();
			
			SettingEntity setting = (SettingEntity) criteria.uniqueResult();
			if(setting != null){
				signoutTime = timeFormat.parse(setting.getValue());
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new MyException("数据库访问错误");
		}finally{
			if(session != null){
				session.close();
			}	
		}
		return signoutTime;
	}
	
	public void setSignoutTime(Date signoutTime) throws MyException {
		Session session = null;
		SettingEntity setting = new SettingEntity();
		try {
			session = sessionFactory.openSession();	
			Transaction tran = session.beginTransaction();  
			setting.setKey(SIGNOUT_TIME);
			setting.setValue(timeFormat.format(signoutTime));
			session.update(setting);
			tran.commit();
		} catch (Exception e) {
			e.printStackTrace();
			throw new MyException("数据库访问错误");
		}finally{
			if(session != null){
				session.close();
			}	
		}
	}
}
